/**
 * Copyright (c) 2011 ~ 2013 Deepin, Inc.
 *               2011 ~ 2013 jouyouyun
 *
 * Author:      jouyouyun <jouyouwen717@gmail.com>
 * Maintainer:  jouyouyun <jouyouwen717@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 **/

package main

import (
        "fmt"
        "os"
        "os/user"
)

const (
        THEMES_DIR = "/usr/share/themes"
        ICONS_DIR  = "/usr/share/icons"
)

func getHomeDir() string {
        u, err := user.Current()
        if err != nil {
                fmt.Println("Get current user info failed:", err)
                panic(err)
        }
        return u.HomeDir
}

func getValidGtkThemes() []string {
        localDir := getHomeDir() + "/.themes"
        dirs := []string{THEMES_DIR, localDir}
        conditions := []string{"gtk-2.0", "gtk-3.0"}

        list := getValidThemes(dirs, conditions)
        fmt.Println("Valid Gtk Themes List:", list)

        return list
}

func getValidWindowThemes() []string {
        localDir := getHomeDir() + "/.themes"
        dirs := []string{THEMES_DIR, localDir}
        conditions := []string{"metacity-1"}

        list := getValidThemes(dirs, conditions)
        fmt.Println("Valid Window Themes List:", list)

        return list
}

func getValidIconThemes() []string {
        localDir := getHomeDir() + "/.icons"
        dirs := []string{ICONS_DIR, localDir}
        conditions := []string{"index.theme"}

        list := getValidThemes(dirs, conditions)
        fmt.Println("Valid Icons Themes List:", list)

        return list
}

func getValidCursorThemes() []string {
        localDir := getHomeDir() + "/.icons"
        dirs := []string{ICONS_DIR, localDir}
        conditions := []string{"cursors"}

        list := getValidThemes(dirs, conditions)
        fmt.Println("Valid Cursor Themes List:", list)

        return list
}

func getValidThemes(dirs, conditions []string) []string {
        valid := []string{}
        for _, dir := range dirs {
                f, err := os.Open(dir)
                if err != nil {
                        fmt.Printf("Open '%s' failed: %s\n", dir, err)
                        continue
                }
                defer f.Close()

                infos, err1 := f.Readdir(0)
                if err1 != nil {
                        fmt.Printf("ReadDir '%s' failed: %s\n", dir, err)
                        continue
                }

                for _, info := range infos {
                        if !info.IsDir() {
                                continue
                        }

                        if filterTheme(dir+"/"+info.Name(),
                                conditions) {
                                valid = append(valid, info.Name())
                        }
                }
        }

        return valid
}

func filterTheme(dir string, conditions []string) bool {
        f, err := os.Open(dir)
        if err != nil {
                fmt.Printf("Open '%s' failed: %s\n", dir, err)
                return false
        }
        defer f.Close()

        names, err1 := f.Readdirnames(0)
        if err1 != nil {
                fmt.Printf("ReadDir '%s' failed: %s\n", dir, err)
                return false
        }

        cnt := 0
        for _, name := range names {
                for _, condition := range conditions {
                        if name == condition {
                                cnt++
                                break
                        }
                }
        }

        if cnt == len(conditions) {
                return true
        }

        return false
}
